import { ActionReducerMapBuilder, AsyncThunk, createAsyncThunk, createSlice, PayloadAction } from '@reduxjs/toolkit';
import { Employee, EmployeeType, MessageParams, PageEmp } from '@/type/employeeType';
import { addEmployeeAPI, assignEmployeeRoleAPI, deleteEmployeeAPI, fetchEmployeeAPI, findEmployeeAPI, sendMessageAPI, updateEmployeeAPI } from '@/api/employeeAPI';
import { AsyncThunkConfig } from '@reduxjs/toolkit/dist/createAsyncThunk';


type InitialState = {
  employees: Employee[];
  employeeDetail: Employee
  total: number
}

const initialState: InitialState = {
  employees: [],
  total: 0,
  employeeDetail: {} as Employee,
};

/**
 * @description 获取员工列表的异步函数
 * */
export const fetchEmployee: any = createAsyncThunk('fetchEmployee', async (page: PageEmp, { rejectWithValue }) => {
  try {
    const { data } = await fetchEmployeeAPI(page);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

/**
 * @description 删除员工的异步函数
 * */
export const deleteEmployee: any = createAsyncThunk('deleteEmployee', async (id: number, { rejectWithValue }) => {
  try {
    const { message } = await deleteEmployeeAPI(id);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 新增员工的异步函数
 * */
export const addEmployee: AsyncThunk<string, Employee, AsyncThunkConfig> = createAsyncThunk('addEmployee', async (formData: Employee, { rejectWithValue }) => {
  try {
    const { message } = await addEmployeeAPI(formData);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 获取员工信息的异步函数
 * */
export const findEmployee: AsyncThunk<Employee, number, AsyncThunkConfig> = createAsyncThunk('findEmployee', async (id: number, { rejectWithValue }) => {
  try {
    const { data } = await findEmployeeAPI(id);
    return data;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 修改员工基本信息的异步函数
 * */

export const updateEmployee: AsyncThunk<string, Employee, AsyncThunkConfig> = createAsyncThunk('updateEmployee', async (formData: Employee, { rejectWithValue }) => {
  try {
    const { message } = await updateEmployeeAPI(formData);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});


/**
 * @description 分配员工角色的异步函数
 * */
export const assignEmployeeRole: AsyncThunk<string, { id: number, roleIds: number[] }, AsyncThunkConfig> = createAsyncThunk('assignEmployeeRole', async ({ id, roleIds }, { rejectWithValue }) => {
  try {
    const { message } = await assignEmployeeRoleAPI(id, roleIds);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }

});

/**
 * @description 群发通知的异步函数
 * */
export const sendMessage: AsyncThunk<string, MessageParams, AsyncThunkConfig> = createAsyncThunk('sendMessage', async (messageData: MessageParams, { rejectWithValue }) => {
  try {
    const { message } = await sendMessageAPI(messageData);
    return message;
  } catch (error: any) {
    return rejectWithValue(error.message);
  }
});

export const employeeStore = createSlice({
  name: 'employeeStore',
  initialState,
  reducers: {},
  extraReducers(builder: ActionReducerMapBuilder<InitialState>) {
    builder.addCase(fetchEmployee.fulfilled, (state: InitialState, action: PayloadAction<EmployeeType>) => {
      const { payload } = action;
      state.employees = payload.rows;
      state.total = payload.total;
    });
    builder.addCase(findEmployee.fulfilled, (state: InitialState, action: PayloadAction<Employee>) => {
      const { payload } = action;
      state.employeeDetail = payload;
    });
  },
});


export default employeeStore.reducer;
